package com.xzh.homeliving.commodity.controller;

import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzh.homeliving.commodity.entity.AttrAtRelationEntity;
import com.xzh.homeliving.commodity.service.AttrAtRelationService;
import com.xzh.homeliving.commodity.service.CategoryService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xzh.homeliving.commodity.entity.AtEntity;
import com.xzh.homeliving.commodity.service.AtService;
import com.xzh.common.utils.PageUtils;
import com.xzh.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性表
 *
 * @author xzh
 * @email ranpis02@qq.com
 * @date 2024-01-31 10:42:04
 */
@RestController
@RequestMapping("commodity/at")
public class AtController {
    @Resource
    private AtService atService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrAtRelationService attrAtRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:at:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = atService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("commodity:at:info")
    public R info(@PathVariable("attrId") Long attrId){
		AtEntity at = atService.getById(attrId);

        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(at.getCategoryId());
        at.setCascadedCategoryId(cascadedCategoryId);

        // 如果属性为基本属性，那么还需要我们查询其所在的组，并将属性所属分组插入
        if(at.getAttrType() != null && at.getAttrType() == 1) {
            Long attrGroupId = attrAtRelationService.getOne(new QueryWrapper<AttrAtRelationEntity>().eq("attr_id", at.getAttrId())).getAttrGroupId();
            at.setAttrGroupId(attrGroupId);
        }

        return R.ok().put("at", at);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:at:save")
    public R save(@RequestBody AtEntity at){
		//atService.save(at);
        atService.saveAttrRelation(at);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:at:update")
    public R update(@RequestBody AtEntity at){
		atService.updateAttrRelation(at);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:at:delete")
    public R delete(@RequestBody Long[] attrIds){
		atService.deleteAttrRelation(attrIds);

        return R.ok();
    }

    @RequestMapping("/base/list/{categoryId}")
    public R listBase(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId){
        PageUtils page = atService.queryBaseAttrPage(params, categoryId);
        return R.ok().put("page", page);
    }

    @RequestMapping("/sale/list/{categoryId}")
    public R listSale(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId){
        PageUtils page = atService.querySaleAttrPage(params, categoryId);
        return R.ok().put("page", page);
    }
}
